package com.walkline.util;

import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.system.GPRSInfo;
import net.rim.device.api.system.GPRSInfo.GPRSCellInfo;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.system.WLANInfo;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class Function
{
    public static void errorDialog(final String message)
    {
        UiApplication.getUiApplication().invokeAndWait(new Runnable()
        {
            public void run()
            {
                Dialog.alert(message);
            } 
        });
    }

    public static Bitmap CropAndResizeBitmap(EncodedImage image, int maxWidth, int maxHeight)
    {
    	int width = image.getWidth();
    	int height = image.getHeight();
    	int bigger = width > height ? width : height;
    	int smaller = width > height ? height : width;
    	Bitmap newImage = image.getBitmap();

    	if (width == maxWidth && height == maxHeight) {return image.getBitmap();}

    	if (Math.abs(width - height) > 3)
    	{
        	newImage = new Bitmap(smaller, smaller);
        	Graphics graphics = Graphics.create(newImage);

        	int left = width > height ? (bigger - smaller) / 2 : 0;
        	int top = width < height ? (bigger - smaller) / 2 : 0;
        	graphics.drawBitmap(0, 0, smaller, smaller, image.getBitmap(), left, top);    		
    	}

    	//int ratio = 10000 * maxHeight / image.getHeight();
    	//int desiredWidth = maxWidth * ratio / 10000;
    	//int desiredHeight = maxHeight * ratio / 10000;

    	return GPATools.ResizeTransparentBitmap(newImage, maxWidth, maxHeight, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FIT);
    }

	public static Bitmap bestFit(EncodedImage image, int maxWidth, int maxHeight)
	{
		int width = image.getWidth();
		int height = image.getHeight();

		if (width <= maxWidth && height <= maxHeight) {return image.getBitmap();}

		int ratiow = 10000 * maxWidth / width;
		int ratioh = 10000 * maxHeight / height;

		int ratio = ratioh;//Math.min(ratiow, ratioh);

		int desiredWidth = width * ratio / 10000;
		int desiredHeight = height * ratio / 10000;

		//return image.getBitmap();
		//return setSizeImage(image, desiredWidth, desiredHeight);
		return GPATools.ResizeTransparentBitmap(image.getBitmap(), desiredWidth, desiredHeight, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FIT);
	}

	public static EncodedImage bestFitEncodedImage(EncodedImage image, int maxWidth, int maxHeight)
	{
		int w = image.getWidth();
		int h = image.getHeight();
		
		if(w<=maxWidth && h<=maxHeight) {return image;}
		
		int ratiow = 10000 * maxWidth / w;
		int ratioh = 10000 * maxHeight / h;
		
		int ratio = Math.min(ratiow, ratioh);
		
		int desiredWidth = w * ratio / 10000;
		int desiredHeight = h * ratio / 10000;
		
		return setSizeEncodedImage(image, desiredWidth, desiredHeight);
	}

	private static EncodedImage setSizeEncodedImage(EncodedImage image, int width, int height)
	{
		EncodedImage result = null;

		int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
		int currentHeightFixed32 = Fixed32.toFP(image.getHeight());

		int requiredWidthFixed32 = Fixed32.toFP(width);
		int requiredHeightFixed32 = Fixed32.toFP(height);

		int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
		int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);

		result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
		
		return result;
	}

	public static void showExitDialog()
    {
		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run()
			{
				String[] yesno = {"是 (Y\u0332)", "否 (N\u0332)"};
				Dialog showDialog = new Dialog("确认退出？", yesno, null, 1, Bitmap.getPredefinedBitmap(Bitmap.QUESTION), 0);

				showDialog.doModal();
				if (showDialog.getSelectedValue() == 0) {System.exit(0);}
			}
		});
    }

//	public static boolean hasAnyConnection()
//	{
//		
//	}

	public static boolean isWiFiConnected()
	{
		return (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED && RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN));
	}
}